package com.hxg.server.exception;

import com.hxg.server.pojo.Resp.RespBean;
import org.apache.ibatis.session.SqlSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 * @author Hexg
 * @date 2022/3/1 20:22
 */
@RestControllerAdvice
public class GlobalException {
    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);

    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e){
        if (e instanceof SQLIntegrityConstraintViolationException){
            logger.error("{}============>出现异常",e.getMessage());
            return RespBean.error("该数据库有关联数据，操作失败！");
        }
        logger.error("{}============>出现异常",e.getMessage());
        return RespBean.error("数据库异常，操作失败！");
    }
}
